package cn.harry.modular.society.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.component.security.utils.SecurityUtils;
import cn.harry.modular.society.domain.Yonghuhuodong;
import cn.harry.modular.society.service.YonghuhuodongService;
import cn.harry.modular.society.vo.YonghuhuodongVO;
import cn.harry.modular.system.domain.SysUser;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Tag(name = "用户活动关系接口")
@RestController
@RequestMapping("/yonghuhuodong")
@RequiredArgsConstructor
public class YonghuhuodongController {


    private final YonghuhuodongService yonghuhuodongService;

    /**
     * 分页查询列表
     */
    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
    public R<IPage<YonghuhuodongVO>> page(@ParameterObject Page<Yonghuhuodong> page, @ParameterObject YonghuhuodongVO entity) {
        SysUser user = SecurityUtils.getSysUser();
        if (!SecurityUtils.isRoot()) {
            long userId = user.getId();
            entity.setUserId(userId);
            IPage<YonghuhuodongVO> iPage = yonghuhuodongService.pageVo(page, entity);
            return R.success(iPage);
        }
        IPage<YonghuhuodongVO> iPage = yonghuhuodongService.pageVo(page, entity);
        return R.success(iPage);
    }

    /**
     * 根据id获取详情
     */
    @Operation(summary = "根据id获取详情")
    @GetMapping(value = "/{id}")
    public R<Yonghuhuodong> getById(@PathVariable Long id) {
        return R.success(yonghuhuodongService.getById(id));
    }

    /**
     * 新增
     */
    @Operation(summary = "新增")
    @SysLog(title = "t_Yonghuhuodong", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> save(@RequestBody Yonghuhuodong entity) {
        return yonghuhuodongService.save(entity) ? R.success() : R.failed();
    }

    /**
     * 更新
     */
    @Operation(summary = "更新")
    @SysLog(title = "t_Yonghuhuodong", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Integer> update(@RequestBody Yonghuhuodong entity) {
        return yonghuhuodongService.updateById(entity) ? R.success() : R.failed();
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @SysLog(title = "t_Yonghuhuodong", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {
        return yonghuhuodongService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.failed();
    }
}
